Yacon.widget.TDrop = function(element){
	this.element = element;
};

Yacon.widget.TDrop.prototype = {
	/* 获取HTML信息 */	
	getHtml: function(){
		
		var html = "";
		
		this.form = this.parent;
		
		if(this.parent.page.windows){
			this.dataSet = this.parent.page.windows.dataSet;
		}
		
		if(!Yacon.isTrue(this.element.lableVisible) 
		&& !Yacon.isTrue(this.element.hintVisible)
		&& !Yacon.isTrue(this.element.isCheck)){
			html += "<div>";
		}else{
			html += "<div class='input-group'>";
		}
		
		var lableClass = "";
		
		if(this.isEnable() || this.parent.view.isStatic){
			lableClass += "bk-bg-primary";
		}else{
			lableClass += "bk-bg-brown";
		}
		
		if(this.element.btnState){
			lableClass = "bk-bg-"+this.element.btnState;
		}
		
		if(Yacon.isTrue(this.element.lableVisible)){
			
			html += "<span class='input-group-btn "+lableClass+"' "+this.getColorStyle()+" >";	
			
			html += new Yacon.widget.TButton(this.element.labelMenu).getFormHtml();
			
			/*html += "<button class='btn "+lableClass+"' "+this.getColorStyle()+" type='button'>";
			
			html += this.getLableHtml();
			
			html += "</button>";*/
			
			html += "</span>";
			
		}
		
	
		html += "<select name='"+this.element.name+"' class='form-control lily-field' ";
			
		if(this.element.blank){
			html += " placeholder='"+this.element.blank+"' ";
		}
		
		html += " field-name='"+this.element.name+"' ";
		
		html += " field-type='drop' ";
		
		if(!Yacon.isEmpty(this.element.relaID)){
			html += " rela-id='"+this.element.relaID+"' ";
		}
			
		if(!this.isEnable()){
			html += " disabled ";
		}
		
		html += " field-is-null='"+this.element.isNull+"' "
		html += " field-max-length='"+this.element.maxLength+"' "
		
		var labelText = this.element.text;
		if(labelText.endWith(" *")){
			labelText = labelText.substring(0,labelText.length-2);
		}else if(labelText.endWith("*")){
			labelText = labelText.substring(0,labelText.length-1);
		}
		html += " field-label='"+labelText+"' ";
		
		html += " > ";
		
		/* 选项 */
		var size = this.element.data.length;
		for(var i=0;i<size;i++){
			html += "<option value='"+this.element.data[i].key+"' "+this.getDefaultValue(this.element.data[i].key)+" >"+this.element.data[i].value+"</option>";
		}
		
		if(size != 0){
			this.activeText = this.element.data[0].value;
			this.activeValue = this.element.data[0].key;
		}else{
			
			var relaID = this.element.name;
			var relaObj = this.parent.dropItems.getFieldValue(relaID);
			
			if(!Yacon.isEmpty(relaObj)){
				html += this.addOwnerDrop(relaObj);	
			}
			
		}
		
		html += "</select>";
		
		if(Yacon.isTrue(this.element.hintVisible)){
			
			for(var i=0;i < this.element.hintMenu.length;i++){
				
				var curButton = this.element.hintMenu[i];
				
				html += "<span class='input-group-btn "+lableClass+"' "+this.getButtonColorStyle(curButton)+" >";
				
				html += new Yacon.widget.TButton(curButton).getFormHtml();
				
				html += "</span>";
				
			}
			
		}
		
		html += "</div>";
		
		return html;
	},
	
	isEnable: function(){
		
		this.page = this.parent.page;
		var state = this.page.element.parameter.state;
		
		if(Yacon.isEmpty(state)){
			state = "active";
		}
		
		if(!this.element.isEnable){
			return false;
		}
		
		if(state == "insert"){
			return this.element.isInsert;
		}else if(state == "edit"){
			return this.element.isEdit;
		}else if(state == "past"){
			return false;
		}	
		else{
			return true;
		}
		
	},
	
	getLableHtml: function(){
		
		var html = "";
		var lable = this.element.text;
		var lableIco = this.element.ico;
		var lableType = this.element.viewType;
		
		if(Yacon.isEmpty(lableIco)){
			lableIco = "glyphicon glyphicon-tag";
		}
		
		if(lableType == "all"){
			html += "<span class='"+lableIco+"'></span>&nbsp;&nbsp;"+lable;
		}
		else if(lableType == "ico"){
			html += "<span class='"+lableIco+"'></span>";
		}
		else if(lableType == "label"){
			html += lable;
		}
		
		return html;
	},
	
	addEvent: function(){
		
		//联动处理
		var relaID = this.element.relaID;
		var elementID = this.element.name.trim().toUpperCase();
		var self = this;
		
		if(!Yacon.isEmpty(relaID)){
			
			$(".form-control.lily-field[field-name='"+elementID.trim().toUpperCase()+"']").unbind("change");
			$(".form-control.lily-field[field-name='"+elementID.trim().toUpperCase()+"']").change(function(){
				
				var form = self.parent;
				
				self.activeValue = form.value(elementID.trim().toUpperCase());
				
				self.addRelaDrop(self);
				
			});
		}
		
		//校验处理
		
		
	},
	
	getDefaultValue: function(value){
		
		this.page = this.parent.page;
		var state = this.page.element.parameter.state;
		
		if(Yacon.isEmpty(state)){
			state = "active";
		}
		
		if(!Yacon.isEmpty(this.element.commonVal)){			
			if(value == this.element.commonVal){
				return "selected";
			}else{
				return "";
			}
		}
		
		else{
			
			if(state == "insert"){
				if(!Yacon.isEmpty(this.element.insertVal)){	
					if(value == this.element.insertVal){
						return "selected";
					}else{
						return "";
					}	
				}else{
					return "";
				}
			}
			
			else if(state == "edit"){
				if(!Yacon.isEmpty(this.element.editVal)){			
					if(value == this.element.editVal){
						return "selected";
					}else{
						return "";
					}
				}else{
					return "";
				}
			}
			
			else {
				return "";
			}
			
		}
	},
	
	getDefaultValueStr: function(){
		
		this.page = this.parent.page;
		var state = this.page.element.parameter.state;
		
		if(Yacon.isEmpty(state)){
			state = "active";
		}
		
		if(!Yacon.isEmpty(this.element.commonVal)){			
			return " "+this.element.commonVal+" ";		
		}
		
		else{
			
			if(state == "insert"){
				if(!Yacon.isEmpty(this.element.insertVal)){			
					return ""+this.element.insertVal+" ";		
				}else{
					return "";
				}
			}
			
			else if(state == "edit"){
				if(!Yacon.isEmpty(this.element.editVal)){			
					return ""+this.element.editVal+"";	
				}
				
				else if(this.dataSet){
					var retValue = this.dataSet.getDataSetValue(this.element.name);
					if(retValue){
						return retValue;
					}else{
						return "";
					}
				}
				
				else{
					return "";
				}
			}
			
			else {
				return "";
			}
			
		}
	},
	
	addRelaDrop: function(dropObj){
		
		var form = dropObj.parent;
		var relaID = dropObj.element.relaID;
		var relaMethod = dropObj.element.relaMethod;
		var elementID = dropObj.element.name;
		
		var relaDropObj = form.find(relaID);
		
		if(!Yacon.isEmpty(relaID)){
			
			var sendSet = new Yacon.data.DataSet(form.page);
			sendSet.setBody(elementID,form.value(elementID));
			sendSet.switchMessage(relaMethod);
			
			var active = $(".form-control.lily-field[field-name='"+relaID+"']");
			
			active.empty();
			
			var html = "";
			
			while(sendSet.next()){
				
				var fieldText = sendSet.getDataSetValue("text");
				var fieldValue = sendSet.getDataSetValue("value");
				
				html += "<option value='"+fieldValue+"'>"+fieldText+"</option>";
				
			}
			
			active.append(html);
			
			active.find("option").eq(0).prop("selected","true");
			
			this.addRelaDrop(relaDropObj);
			
		}
		
	},
	
	addOwnerDrop: function(dropObj){
				
		var form = dropObj.parent;
		var relaID = dropObj.element.relaID;
		var relaMethod = dropObj.element.relaMethod;
		var elementID = dropObj.element.name;
		
		if(!Yacon.isEmpty(relaID)){
			
			var sendSet = new Yacon.data.DataSet(form.page);
			
			if(Yacon.isEmpty(dropObj.getDefaultValueStr())){
				sendSet.setBody(elementID,dropObj.activeValue.trim());
			}else{
				sendSet.setBody(elementID,dropObj.getDefaultValueStr().trim());
			}
				
			sendSet.switchMessage(relaMethod);
			
			var html = "";
			
			var isFirst = true;
			
			while(sendSet.next()){
				
				var fieldText = sendSet.getDataSetValue("text");
				var fieldValue = sendSet.getDataSetValue("value");
				
				if(isFirst){
					this.activeText = fieldText;
					this.activeValue = fieldValue;
					isFirst = false;
				}
			
				html += "<option value='"+fieldValue+"' "+this.getDefaultValue(fieldValue)+" >"+fieldText+"</option>";
				
			}
			
			return html;
			
		}
		
		return "";
		
	},
	
	getColorStyle: function(){
		
		var html = "";
		
		var btnColor = "";
		
		if(!Yacon.isEmpty(this.element.fontColor)){
			btnColor += "color:"+this.element.fontColor+" !important;";
		}
		
		if(!Yacon.isEmpty(this.element.backColor)){
			btnColor += " background-color:"+this.element.backColor+" !important;";
			btnColor += " border-color:"+this.element.backColor+" !important;";
		}
		
		if(!Yacon.isEmpty(btnColor)){
			html += " style='"+btnColor+"' ";
		}
		
		return html;
		
	},
	
	getButtonColorStyle: function(button){
		
		var html = "";
		
		var btnColor = "";
		
		if(!Yacon.isEmpty(button.fontColor)){
			btnColor += "color:"+button.fontColor+" !important;";
		}
		
		if(!Yacon.isEmpty(button.backColor)){
			btnColor += " background-color:"+button.backColor+" !important;";			
		}
		
		btnColor += " border-right:2px solid #FFFFFF !important; ";
		
		if(!Yacon.isEmpty(btnColor)){
			html += " style='"+btnColor+"' ";
		}
		
		return html;
		
	}
	
};